<template>
  <div class="map-wrap">
    <div class="flex">
      <div class="button-wrap">
        <!-- 绘制按钮 -->
        <el-button
          size="small"
          type="primary"
          icon="el-icon-edit"
          @click="handleDraw"
        >绘制</el-button>
        <!-- 完成绘制按钮 -->
        <el-button size="small" icon="el-icon-check" @click="handelFinishDraw">完成</el-button>
        <!-- 重置绘制按钮 -->
        <el-button
          size="small"
          icon="el-icon-refresh-left"
          @click="handleClearDraw"
        >重置</el-button>
      </div>
      <!-- 颜色选择器 -->
      <div class="picker-color" v-if="isediting">
        <div id="text">选择颜色</div>
        <span
          v-for="item in colors"
          :key="item.code"
          :class="[
            'color' + item.code,
            drawColor == item.value ? 'active' : '',
          ]"
          @click="handleChangeColor(item.value)"
          :style="{ backgroundColor: item.value }"
        >
          <i v-if="drawColor == item.value" class="el-icon-check"></i>
        </span>
      </div>
    </div>
    <div style="width: 700px; height: 500px; padding-left: calc(50% - 600px)">
      <div id="container"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Map",
  data() {
    return {
      map: null, // 地图实例
      drawingManager: null, // 绘制管理器
      polygon: null, // 多边形覆盖物
      drawColor: "#2A8DFF", // 绘制颜色
      colors: [
        { code: 1, value: "#FF6B36" },
        { code: 2, value: "#FFAD29" },
        { code: 3, value: "#FFDA21" },
        { code: 4, value: "#29E98F" },
        { code: 5, value: "#1EEDE6" },
        { code: 6, value: "#2A8DFF" },
        { code: 7, value: "#CC16EF" },
        { code: 8, value: "#F53ABD" },
      ],
      areaPoint: [], // 存储多边形路径的数组
      isediting: false, // 是否处于编辑状态
    };
  },


  methods: {
    initMap() {
      // 创建地图实例
      this.map = new AMap.Map("container", {
        zoom: 11, // 缩放级别
        center: [116.397428, 39.90923], // 地图中心点坐标
        viewMode: "3D", // 使用3D视图
      });

      // 初始化绘制管理器
      this.drawingManager = new AMap.MouseTool(this.map);
    },

    // 开始绘制
    handleDraw() {
      this.isediting = true;
      this.drawingManager.polygon({
        strokeColor: "#FFF",
        strokeOpacity: 1,
        strokeWeight: 3,
        fillColor: this.drawColor,
        fillOpacity: 0.5,
      });
      this.drawingManager.on("draw", this.onDraw); // 绑定绘制完成事件
    },

    // 完成绘制
    handelFinishDraw() {
      this.isediting = false;
      this.drawingManager.close(false); // 关闭绘制工具，不清除绘制结果
    },

    // 重置绘制
    handleClearDraw() {
      this.isediting = false;
      if (this.drawingManager) {
        this.drawingManager.close(true); // 关闭绘制工具，并清除绘制结果
      }
      if (this.polygon) {
        this.map.remove(this.polygon); // 移除多边形覆盖物
        this.polygon = null;
      }
      this.areaPoint = []; // 清空路径数组
    },

    // 绘制完成事件处理函数
    onDraw(event) {
      console.log(event)
      const path = event.obj.getPath();
      const lnglats = path.map((point) => [point.lng, point.lat]);
      this.areaPoint = lnglats; // 更新 paths 数组
      this.$emit("input", this.areaPoint);
      if (this.polygon) {
        this.map.remove(this.polygon); // 移除旧的多边形覆盖物
      }
      this.polygon = new AMap.Polygon({
        path: lnglats,
        strokeColor: "#FFF",
        strokeWeight: 3,
        fillColor: this.drawColor,
        fillOpacity: 0.5,
      });
      console.log(this.polygon)
      this.map.add(this.polygon); // 将新多边形添加到地图上
    },

    // 切换颜色
    handleChangeColor(color) {
      this.drawColor = color;
      if (this.polygon) {
        this.polygon.setOptions({
          fillColor: this.drawColor,
        });
      }
    },
  },

  created() {
    this.$nextTick(() => {
      this.initMap();
    });
  },
};
</script>

<style scoped>
.map-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}
.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 50px;
}
.button-wrap {
  display: flex;
  align-items: center;
}
.picker-color {
  display: flex; /* 或者使用 display: block; 根据需要 */
  align-items: center;
  margin-left: 10px; /* 根据需要添加外边距 */
}

#text {
  margin-right: 10px;
  font-size: 14px; /* 确保文本可见，可能需要设置字体大小 */
  color: #333; /* 设置文本颜色 */
}

.color1,
.color2,
.color3,
.color4,
.color5,
.color6,
.color7,
.color8 {
  display: inline-block;
  width: 24px;
  height: 24px;
  margin-left: 5px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #ccc; /* 添加边框以提高可见性 */
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.2);
}

.active {
  border: 2px solid #fff;
}
#container {
  width: 100%;
  height: 500px;
  margin: 20px 0;
}
</style>
